﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using ECommerce.Controls;

namespace ECommerce.Customer.Controls
{
	using Data.Entity;
	using Service;
	using System.Web.Security;

	public partial class AddressManage : BaseUserControl
	{
		private MembershipUser _user;
		private IAddressService _AddressService;

		protected void Page_Load(object sender, EventArgs e)
		{
			if (SiteContext.Current == null)
			{
				Server.Transfer("~/Login.aspx");
			}

			if (!IsPostBack)
			{
				this.LinkButtonCreateNewAddress.Text = GetLocalResourceString("AddressManage.AddNewAddressLink");
				ButtonSaveAddress_Create.Text = GetLocalResourceString("ECommerce.SaveButtonText");
				ButtonCancel_Create.Text = GetLocalResourceString("ECommerce.CancelButtonText");

				BindExistAddresses();
			}
		}

		protected void RepeaterAddresses_ItemCommand(object source, RepeaterCommandEventArgs e)
		{
			if (e.CommandName == "UpdateAddress")
			{
				int currentAddressID = 0;
				if (int.TryParse(e.CommandArgument.ToString(), out currentAddressID))
				{
					var textFirstName = (TextBox)e.Item.FindControl("TextFirstName");
					var textLastName = (TextBox)e.Item.FindControl("TextLastName");
					var dropDownListCountryProvCitySel = (CountryProvinceCitySelector)e.Item.FindControl("CountryProvCitySel");
					var textPhoneNumber = (TextBox)e.Item.FindControl("TextPhoneNumber");
					var textMobileNumber = (TextBox)e.Item.FindControl("TextMobileNumber");
					var textZipCode = (TextBox)e.Item.FindControl("TextZipCode");
					var textAddressDetail = (TextBox)e.Item.FindControl("TextAddressDetail");

					if (_AddressService == null)
					{
						_AddressService = new AddressService();
					}
					var theAddress = GetTheAddress(_AddressService, currentAddressID);

					if (theAddress != null)
					{
						theAddress.Address1 = textAddressDetail.Text;
						theAddress.CityID = dropDownListCountryProvCitySel.SelectedCityID;
						theAddress.CountryID = dropDownListCountryProvCitySel.SelectedCountryID;
						theAddress.FirstName = textFirstName.Text.Trim();
						theAddress.LastName = textLastName.Text.Trim();
						theAddress.MobileNumber = textMobileNumber.Text.Trim();
						theAddress.PhoneNumber = textPhoneNumber.Text.Trim();
						theAddress.StateProvinceID = dropDownListCountryProvCitySel.SelectedProvinceID;
						theAddress.UpdatedOn = DateTime.Now;
						theAddress.ZipPostalCode = textZipCode.Text.Trim();

						if (_AddressService.UpdateAddress(theAddress) > 0)
						{
							BindExistAddresses();
						}
					}
				}
			}
			else if (e.CommandName == "DeleteAddress")
			{
				int currentAddressID = 0;
				if (int.TryParse(e.CommandArgument.ToString(), out currentAddressID))
				{
					if (_AddressService == null)
					{
						_AddressService = new AddressService();
					}
					
					if (_AddressService.DeleteAddress(currentAddressID) > 0)
					{
						BindExistAddresses();
					}
				}
			}
		}

		protected void RepeaterAddresses_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			if (e.Item.DataItem != null)
			{
				var theAddress = e.Item.DataItem as Address;
				var dropDownListCountryProvCitySel = (CountryProvinceCitySelector)e.Item.FindControl("CountryProvCitySel");
				dropDownListCountryProvCitySel.SelectedCountryID = theAddress.CountryID;
				dropDownListCountryProvCitySel.SelectedProvinceID = theAddress.StateProvinceID;
				dropDownListCountryProvCitySel.SelectedCityID = theAddress.CityID;

				var saveButton = (Button)e.Item.FindControl("ButtonSaveAddress");
				saveButton.Text = GetLocalResourceString("ECommerce.SaveButtonText");

				var deleteButton = (Button)e.Item.FindControl("ButtonDeleteAddress");
				deleteButton.Text = GetLocalResourceString("ECommerce.DeleteButton.Text");
			}
		}

		protected void ButtonSaveAddress_Create_Click(object sender, EventArgs e)
		{
			_user = SiteContext.Current.User;

			if (_user != null)
			{
				var theAddress = new Address();

				theAddress.Address1 = TextAddressDetail_Create.Text.Trim();
				theAddress.Address2 = string.Empty;
				theAddress.CityID = CountryProvCitySel_Create.SelectedCityID;
				theAddress.Company = string.Empty;
				theAddress.CountryID = CountryProvCitySel_Create.SelectedCountryID;
				theAddress.CreatedOn = DateTime.Now;
				theAddress.Email = string.Empty;
				theAddress.FaxNumber = string.Empty;
				theAddress.FirstName = TextFirstName_Create.Text.Trim();
				theAddress.IsBillingAddress = false;
				theAddress.LastName = TextLastName_Create.Text.Trim();
				theAddress.PhoneNumber = TextPhoneNumber_Create.Text.Trim();
				theAddress.StateProvinceID = CountryProvCitySel_Create.SelectedProvinceID;
				theAddress.UpdatedOn = DateTime.Now;
				theAddress.UserID = new Guid(_user.ProviderUserKey.ToString());
				theAddress.ZipPostalCode = TextZipCode_Create.Text.Trim();
				theAddress.MobileNumber = TextMobileNumber_Create.Text.Trim();

				if (_AddressService == null)
				{
					_AddressService = new AddressService();
				}

				if (_AddressService.CreateNewAddress(theAddress) > 0)
				{
					BindExistAddresses();
				}
			}
		}

		private Address GetTheAddress(IAddressService addressService, int addressID)
		{
			if (addressID <= 0)
			{
				return null;
			}

			if (addressService == null)
			{
				addressService = new AddressService();
			}

			if (_user == null)
			{
				_user = SiteContext.Current.User;

				if (_user == null)
				{
					return null;
				}
			}

			Guid theUserID = new Guid(_user.ProviderUserKey.ToString());

			var userAddress = addressService.GetTheUserAddresses(theUserID);

			if (userAddress != null && userAddress.Count > 0)
			{
				return userAddress.Where(e => e.AddressId == addressID).First();
			}

			return null;
		}

		private void BindExistAddresses()
		{
			if (_user == null)
			{
				_user = SiteContext.Current.User;
			}

			if (_user != null)
			{
				if (_AddressService == null)
				{
					_AddressService = new AddressService();
				}

				Guid theUserID = new Guid(_user.ProviderUserKey.ToString());
				var addresses = _AddressService.GetTheUserAddresses(theUserID);

				RepeaterAddresses.DataSource = addresses;
				RepeaterAddresses.DataBind();

				if (addresses != null && addresses.Count > 0)
				{
					emptyaddresscontainer.Style["display"] = "none";
				}
				else
				{
					emptyaddresscontainer.InnerHtml = "<p>" + GetLocalResourceString("AddressManage.EmptyAddressLable") + "</p>";
					emptyaddresscontainer.Style["display"] = "block";
				}
			}
		}
	}
}